import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Cookie from 'vue-cookies'
import Store from '../store'
import {
	Message
} from 'element-ui';
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject)
		return originalPush.call(this, location, onResolve, onReject);
	return originalPush.call(this, location).catch((err) => err);
}

Vue.use(VueRouter)

const routes = [{
	path: '/',
	alias: '/home',
	name: '/',
	component: Home,
}, {
	path: '/login',
	name: '/login',
	component: () => import('../views/login/index.vue'), //登录,
}, {
	path: '/regPhone',
	name: '/regPhone',
	component: () => import('../views/login/regPhone.vue'), //手机号注册,
}, {
	path: '/regEmail',
	name: '/regEmail',
	component: () => import('../views/login/regEmail.vue'), //邮箱注册,
}, {
	path: '/language',
	name: '/language',
	component: () => import('../views/my/language.vue'), //设置语言,
}, {
	path: '/aboutUs',
	name: '/aboutUs',
	component: () => import('../views/my/aboutUs.vue'), //关于我们,
},{
	path: '/userAgreement',
	name: '/userAgreement',
	component: () => import('../views/login/userAgreement.vue'), //用户协议,
}, {
	path: '/privacyPolicy',
	name: '/privacyPolicy',
	component: () => import('../views/login/privacyPolicy.vue'), //隐私政策,
}, {
	path: '/forgot',
	name: '/forgot',
	component: () => import('../views/login/forgot.vue'), //忘记密码,
}, {
	path: '/wallet',
	name: '/wallet',
	component: () => import('../views/home/wallet.vue'), //钱包详情,
}, {
	path: '/recharge',
	name: '/recharge',
	component: () => import('../views/home/recharge.vue'), //充值,
}, {
	path: '/transfer',
	name: '/transfer',
	component: () => import('../views/home/transfer.vue'), //转账,
}, {
	path: '/addressBook',
	name: '/addressBook',
	component: () => import('../views/home/addressBook.vue'), //地址簿,
}, {
	path: '/addAddress',
	name: '/addAddress',
	component: () => import('../views/home/addAddress.vue'), //添加地址簿,
}, {
	path: '/Trade',
	name: '/Trade',
	component: () => import('../views/trade/index.vue'), //闪兑,
}, {
	path: '/order',
	name: '/order',
	component: () => import('../views/trade/order.vue'), //闪兑订单,
}, {
	path: '/My',
	name: '/My',
	component: () => import('../views/my/index.vue'), //我的,
}, {
	path: '/userInfo',
	name: '/userInfo',
	component: () => import('../views/my/userInfo.vue'), //用户信息,
}, {
	path: '/setNickname',
	name: '/setNickname',
	component: () => import('../views/my/setNickname.vue'), //修改昵称,
}, {
	path: '/share',
	name: '/share',
	component: () => import('../views/my/share.vue'), //分享,
}, {
	path: '/securitySetting',
	name: '/securitySetting',
	component: () => import('../views/my/securitySetting/index.vue'), //安全设置,
}, {
	path: '/changePassword',
	name: '/changePassword',
	component: () => import('../views/my/securitySetting/changePassword.vue'), //修改密码,
}, {
	path: '/transactionPassword',
	name: '/transactionPassword',
	component: () => import('../views/my/securitySetting/transactionPassword.vue'), //交易密码,
}, {
	path: '/bindPhoneEmail',
	name: '/bindPhoneEmail',
	component: () => import('../views/my/securitySetting/bindPhoneEmail.vue'), //绑定手机号/邮箱,
}, {
	path: '/authentication',
	name: '/authentication',
	component: () => import('../views/my/securitySetting/authentication.vue'), //身份验证,
}, {
	path: '/setPhoneEmail',
	name: '/setPhoneEmail',
	component: () => import('../views/my/securitySetting/setPhoneEmail.vue'), //设置手机号/邮箱,
}, {
	path: '/pricingUnit',
	name: '/pricingUnit',
	component: () => import('../views/my/pricingUnit.vue'), //设置计价货币,
}]



const router = new VueRouter({
	routes,
	mode: 'history'
})


// 游客能访问页面
const touristList = ['/login', '/regPhone', '/regEmail', '/setLang', '/userAgreement', '/privacyPolicy', '/forgot', '/language']

router.beforeEach((to, from, next) => {
	// console.log('to', to.path, from.path, to, from);
	const userInfo = Cookie.get('LOGIN_USER_INFO')
	if (touristList.includes(to.path)) {
		next()
	} else if (!userInfo && to.path != '/login') {
		next('/login')
	} else {
		next()
	}

})

export default router